/******************************************************************************
 * Copyright (C) ShenZhen Powerdata Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳市博安达信息技术股份有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.szboanda.hbdc.gwgl.fwgl.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import com.szboanda.beijing.pure.BaseBusinessService;
import com.szboanda.hbdc.gwgl.fwgl.dao.GwglFwglDAO;
import com.szboanda.hbdc.gwgl.fwgl.exception.GwglFwglException;
import com.szboanda.hbdc.gwgl.fwgl.service.IGwglFwglService;
import com.szboanda.platform.common.component.datahelper.DataHelper;
import com.szboanda.platform.common.framext.mybatits.PlatformPageInfo;
import com.szboanda.platform.common.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * @title: 查询发文管理_业务处理service实现类
 * @fileName: GwglFwglServiceImpl.java
 * @description:
 * @copyright: PowerData Software Co.,Ltd. Rights Reserved.
 * @company: 深圳市博安达信息技术股份有限公司
 * @author: powerdata
 * @date: 2019-09-05 14:50
 * @version: V1.0
 */
@Service("GwglFwglService")
public class GwglFwglServiceImpl extends BaseBusinessService implements IGwglFwglService {

    @Autowired
    private GwglFwglDAO gwglFwglDAO;//注入业务处理DAO


    /**
     * 查询【查询发文管理】业务集合信息
     *
     * @param pageNum   分页数
     * @param pageSize  分页长度
     * @param modelInfo 业务Map集合
     * @return 返回业务集合
     * @throws GwglFwglException
     */
    @Override
    public PlatformPageInfo<Map<String, Object>> queryGwglFwgls(int pageNum, int pageSize, Map<String, Object> modelInfo) throws GwglFwglException {
        try {
            modelInfo = modelHandel(modelInfo);
            DataHelper.startPage(pageNum, pageSize);
            // 排序处理
            List<Map<String, Object>> orders = (List<Map<String, Object>>) modelInfo.get("order");
            StringBuffer orderByStr = new StringBuffer();
            if (CollectionUtils.isNotEmpty(orders)) {
                for (Map<String, Object> order : orders) {
                    String field = MapUtils.getString(order, "field");
                    String direction = MapUtils.getString(order, "direction");
                    orderByStr.append(field).append(" ").append(direction);
                }
            }
            DataHelper.startPage(pageNum, pageSize, orderByStr.toString());

            List<Map<String, Object>> results = gwglFwglDAO.queryGwglFwgls(modelInfo);


            return new PlatformPageInfo<Map<String, Object>>(results);

        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "查询查询发文管理列表异常", e);
            throw new GwglFwglException("查询查询发文管理列表异常", e);
        }
    }

    @Override
    public Map<String, Object> getGwglFwglById(Map<String, Object> modelInfo) throws GwglFwglException {
       return null;
    }

    /**
     * 保存【查询发文管理】业务信息
     *
     * @param model 业务Map集合
     * @return 返回业务集合
     * @throws GwglFwglException
     */
    @Override
    public int saveGwglFwgl(Map<String, Object> model) throws GwglFwglException {
        try {
            model = modelHandel(model);
            int count = 0;
            if (isExist(model)) {
                model.put("XGSJ", new Date(System.currentTimeMillis()));
                count = gwglFwglDAO.updateGwglFwgl(model);
            } else {
                this.createBaseInfo(model);
                model.put("XH", Toolkit.getID());
                model.put("XTXH", Toolkit.getID());
                count = gwglFwglDAO.addGwglFwgl(model);
            }
            return count;
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "保存查询发文管理异常", e);
            throw new GwglFwglException("保存查询发文管理异常", e);
        }
    }

    /**
     * 删除【查询发文管理】业务信息
     *
     * @param infos 业务Map集合
     * @return 返回操作标识符
     * @throws GwglFwglException
     */
    @SuppressWarnings("unchecked")
    @Override
    public int deleteGwglFwgl(Map<String, Object> infos) throws GwglFwglException {
        try {
            List<String> delXhs = new ArrayList<String>();
            List<Map<String, Object>> modelInfos = (List<Map<String, Object>>) infos.get("models");
            for (Map<String, Object> modelInfo : modelInfos) {

                delXhs.add(MapUtils.getString(modelInfo, "XH"));
                delXhs.add(MapUtils.getString(modelInfo, "XTXH"));
            }

            Map<String, Object> info = new HashMap<String, Object>();
            info.put("delXHs", delXhs);
            return gwglFwglDAO.deleteGwglFwgl(info);
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "删除查询发文管理异常", e);
            throw new GwglFwglException("删除查询发文管理异常", e);
        }
    }

    /**
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> getZwFileByXh(Map<String, Object> map) {
        return gwglFwglDAO.getZwFileByXh(map);
    }


    /**
     * 判断【查询发文管理】业务信息是否存在
     *
     * @param model 业务Map集合
     * @return 返回操作标识符
     * @throws GwglFwglException
     */
    private boolean isExist(Map<String, Object> model) {
        boolean result = true;
        if ("".equals(MapUtils.getString(model, "XH"))) {
            result = false;
            return result;
        }
//        if (null == this.getGwglFwglById(model)) {
//            result = false;
//        }
        return result;
    }


    /**
     * 【查询发文管理】业务日期类型处理
     *
     * @param model 业务Map集合
     * @return model 业务Map集合
     * @throws GwglFwglException
     */
    private Map<String, Object> modelHandel(Map<String, Object> model) {
        List<String> columns = new ArrayList<String>();
        for (String columnName : columns) {
            try {
                if (model.get(columnName) instanceof Long) {
                    Date date = new Date((Long) model.get(columnName));
                    model.put(columnName, date);
                } else if (!"".equals(MapUtils.getString(model, columnName))) {

                    DateTime parseDate = DateUtil.parseDate(MapUtils.getString(model, columnName));
                    Date date = new Date(parseDate.getTime());
                    model.put(columnName, date);
                } else {
                    model.put(columnName, null);
                }
            } catch (Exception e) {
                LoggerUtil.error(this.getClass(), "日期类型处理异常！", e);
            }
        }
        return model;
    }

    /**
     * 根据主键查询发文信息
     *
     * @param map
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @throws
     * @author 梁磊
     * @date 2020-01-06 17:49
     */
    @Override
    public Map<String, Object> getFwInfoByXh(Map<String, Object> map) {
        return gwglFwglDAO.getFwdjInfo(map);
    }

}
